package com.ggy.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ggy.dao.UserDao;
import com.ggy.dao.bean.UserBean;
import com.ggy.utils.Log4jUtil;

/**
 * @description TODO
 * @author Michael
 * @createTime 2017年8月14日 上午1:03:36
 * 
 */
public class UserDaoImpl implements UserDao {

	@Override
	public List<UserBean> getUsersbyCondition(int page, int size, Map<Object, Object> params) {
		String sql = BASE_SQL_USER_ROLE;
		if (params != null) {
			sql += whereStr(params);
		}
		sql += " limit ?,?";
		Log4jUtil.info("the sql of getUsersByCondition:" + sql);
		return _getUserBeans(jdbc.query(sql, (page - 1) * size, size));
	}

	@Override
	public int getTotalCount(Map<Object, Object> params) {
		int count = 0;
		String sql = "select count(*) as ncount from sys_user";
		if (params != null) {
			sql += whereStr(params);
		}
		ResultSet rs = jdbc.query(sql);
		try {
			while (rs.next()) {
				count = rs.getInt("ncount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jdbc.relese();
		}
		return count;
	}

}
